package com.cc.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cc.common.utils.PageUtils;
import com.cc.user.dao.UserDao;
import com.cc.user.entity.LoginUserEntity;
import com.cc.user.entity.UserEntity;
import com.cc.user.service.UserService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


@Service("userService")
public class UserServiceImpl extends ServiceImpl<UserDao, UserEntity> implements UserService {

    @Override
    public boolean checkMobileUnique(String phone, int permission) {
        UserDao userDao = this.baseMapper;
        Integer count = userDao.selectCount(new QueryWrapper<UserEntity>().eq("mobile", phone).eq("permission", permission));
        if (count > 0) return true;
        return false;
    }

    @Override
    public UserEntity pwdlogin(LoginUserEntity loginUser) {
        String phone = loginUser.getPhone();
        String password = loginUser.getPassword();
        int permission = loginUser.getPermission();

        //用电话号码在数据库查询该用户
        UserDao userDao = this.baseMapper;
        UserEntity userEntity = userDao.selectOne(new QueryWrapper<UserEntity>().eq("mobile", phone).eq("permission", permission));

        if (userEntity != null){
            //登录成功
            //匹配密码
            String passwordDb = userEntity.getPassword();
            BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
            boolean matches = encoder.matches(password, passwordDb);
            if (matches){
                return userEntity;
            }
        }
        //登录失败(密码错误)
        return null;
    }

    @Override
    public List<UserEntity> getAllPeople(Long permission) {
        UserDao userDao = this.baseMapper;
        List<UserEntity> users = userDao.selectList(new QueryWrapper<UserEntity>().eq("permission",permission));
        return users;
    }

    @Override
    public UserEntity loginByPhone(String phone, Long permission) {
        UserDao userDao = this.baseMapper;
        UserEntity userEntity = userDao.selectOne(new QueryWrapper<UserEntity>().eq("mobile", phone).eq("permission", permission));
        return userEntity;
    }

    @Override
    public int updateInfo(UserEntity userEntity) {
        UserDao userDao = this.baseMapper;
        return userDao.updateById(userEntity);
    }

    @Override
    public Long getCidByPhone(String mobile) {
        UserDao userDao = this.baseMapper;
        UserEntity customer = userDao.selectOne(new QueryWrapper<UserEntity>().eq("mobile", mobile).eq("permission", 2));
        if (customer == null)   return null;
        return customer.getUid();
    }

    @Override
    public String getCnameByPhone(String phone) {
        UserDao userDao = this.baseMapper;
        UserEntity customer = userDao.selectOne(new QueryWrapper<UserEntity>().eq("mobile", phone).eq("permission", 2));
        if (customer == null)   return null;
        return customer.getNickname();
    }

    @Override
    public Page<UserEntity> getAllUserByPermission(Map<String, Object> params, int verifyStatus) {
        QueryWrapper<UserEntity> queryWrapper = new QueryWrapper<>();
        if (verifyStatus != 0 && verifyStatus != 1) {
            queryWrapper.orderByDesc("createtime");
        }else{
            queryWrapper.eq("verify_status",verifyStatus).orderByDesc("createtime");
        }
        int curpage = Integer.parseInt((String) params.get("page"));
        int limit = Integer.parseInt((String) params.get("limit"));
        Page<UserEntity> page = new Page<>(curpage,limit);
        this.baseMapper.selectPage(page, queryWrapper);
        return page;
    }

    @Override
    public PageUtils getUsersByMobilePermission(Map<String, Object> params) {
        String mobile = (String) params.get("mobile");
        String permissionStr = (String) params.get("permission");
        Integer permission = -1;
        if (!permissionStr.equals(""))
            permission = Integer.parseInt(permissionStr);
        String verifyStatusStr = (String) params.get("verify_status");
        Integer verifyStatus = -1;
        if (!verifyStatusStr.equals(""))
            verifyStatus = Integer.parseInt(verifyStatusStr);
        if(mobile == null || permission == null || verifyStatus == null)    return null;

        List<UserEntity> users = this.baseMapper.getUsersByMobilePermission(mobile,permission,verifyStatus);

        int page = Integer.parseInt((String) params.get("page"));
        int limit = Integer.parseInt((String) params.get("limit"));
        int totalCount = users.size();
        List<UserEntity> list = new ArrayList<>();
        for(int i = (page-1)*limit;i<page*limit;i++){
            if(i<=users.size()-1) {
                list.add(users.get(i));
            }
        }
        PageUtils pageUtils = new PageUtils(list, totalCount, limit, page);
        return pageUtils;
    }

    @Override
    public int update(String phone, UserEntity userEntity) {
        UserDao userDao = this.baseMapper;
        return userDao.update(userEntity, new QueryWrapper<UserEntity>().eq("mobile", phone));
    }
}